<?php

/**
 *   OPENSHOP 包装管理程序
 * 
 *   @link        http://baison.com.cn
 *   @copyright   Baison, Inc.
 *   @package     OpenShop
 *   @version     $Id: pack.php,v 1.0 2009/06/19 09:36:58 modified $
 *   @author      FillBag <fillbag@hotmail.com>
 */

define('IN_OS', true);

require(dirname(__FILE__) . '/includes/init.php');

require_once(ROOT_PATH . 'admin/includes/cls_ftp.php');
$ftp = new cls_ftp();
$ftp->setting('pack', 'data/packimg', FALSE); 

$exc = new exchange($os->table("pack"), $db, 'pack_id', 'pack_name');

/*------------------------------------------------------ */
//-- 包装列表
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'list')
{
	admin_priv();
	
    $smarty->assign('ur_here',      '商品包装');
    $smarty->assign('action_link',  array('text' => '添加新包装', 'href'=>'pack.php?act=add'));
    $smarty->assign('full_page',  1);

    $packs_list = packs_list();

    $smarty->assign('packs_list',   $packs_list['packs_list']);
    $smarty->assign('filter',       $packs_list['filter']);
    $smarty->assign('record_count', $packs_list['record_count']);
    $smarty->assign('page_count',   $packs_list['page_count']);

    assign_query_info();
	
	$smarty->display('pack_list.tpl');
}
/*------------------------------------------------------ */
//-- ajax 列表
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'query')
{
    $packs_list = packs_list();
    $smarty->assign('packs_list',    $packs_list['packs_list']);
    $smarty->assign('filter',       $packs_list['filter']);
    $smarty->assign('record_count', $packs_list['record_count']);
    $smarty->assign('page_count',   $packs_list['page_count']);

    $sort_flag  = sort_flag($packs_list['filter']);
    $smarty->assign($sort_flag['tag'], $sort_flag['img']);

    make_json_result($smarty->fetch('pack_list.tpl'), '', array('filter' => $packs_list['filter'], 'page_count' => $packs_list['page_count']));
}
/*------------------------------------------------------ */
//-- 添加新包装
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'add')
{
    /* 权限判断 */
    admin_priv('add_pack');

    $pack['pack_fee'] = 0;
    $pack['free_money'] = 0;

    $smarty->assign('pack',         $pack);
    $smarty->assign('ur_here',      '添加包装');
    $smarty->assign('form_action','insert');
    $smarty->assign('action_link',  array('text' => '包装列表', 'href'=>'pack.php?act=list'));

    assign_query_info();
	
	$smarty->display('pack_info.tpl');
}
if ($_REQUEST['act'] == 'insert')
{
    /* 权限判断 */
    admin_priv('add_pack');

    /*检查包装名是否重复*/
    $is_only = $exc->is_only('pack_name', $_POST['pack_name']);

    if (!$is_only)
    {
        sys_msg(sprintf($_LANG['packname_exist'], stripslashes($_POST['pack_name'])), 1);
    }

    /* 处理图片 */
    if (!empty($_FILES['pack_img']))
    {	
	    $ftp->connect();
	    $affix_data = $ftp->put_up($_FILES['pack_img']);
	    if ($affix_data != false)
	    {
	        $img_name = $affix_data['virtual_path'];
	    }else{
	    	sys_msg('上传包装失败');
	    }
	    $ftp->quit();
    }
    else
    {
        $img_name = '';
    }

    /*插入数据*/
    $sql = "INSERT INTO ".$os->table('pack')."(pack_name, pack_fee, free_money, pack_desc, pack_img)
            VALUES ('$_POST[pack_name]', '$_POST[pack_fee]', '$_POST[free_money]', '$_POST[pack_desc]', '$img_name')";
    $db->query($sql);

    /*添加链接*/
    $link[0]['text'] = '返回包装列表';
    $link[0]['href'] = 'pack.php?act=list';
    $link[1]['text'] = '继续添加包装';
    $link[1]['href'] = 'pack.php?act=add';
    sys_msg($_POST['pack_name'].$_LANG['packadd_succed'],0, $link);
    admin_log($_POST['pack_name'],'add','pack');

}

/*------------------------------------------------------ */
//-- 编辑包装
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'edit')
{
    /* 权限判断 */
    admin_priv('edit_pack');

    $sql = "SELECT pack_id, pack_name, pack_fee, free_money, pack_desc, pack_img FROM " .$os->table('pack'). " WHERE pack_id='$_REQUEST[id]'";
    $pack = $db->GetRow($sql);
    $smarty->assign('ur_here',      $_LANG['pack_edit']);
    $smarty->assign('action_link',  array('text' => '商品包装', 'href'=>'pack.php?act=list&' . list_link_postfix()));
    $smarty->assign('pack',       $pack);
    $smarty->assign('form_action','update');
	
	$smarty->display('pack_info.tpl');
	
}
if ($_REQUEST['act'] == 'update')
{
    /* 权限判断 */
    admin_priv('edit_pack');
    
    if ($_POST['pack_name'] != $_POST['old_packname'])
    {
        /*检查品牌名是否相同*/
        $is_only = $exc->is_only('pack_name', $_POST['pack_name'], $_POST['id']);

        if (!$is_only)
        {
            sys_msg(sprintf($_LANG['packname_exist'], stripslashes($_POST['pack_name'])), 1);
        }
    }

    $param = "pack_name = '$_POST[pack_name]', pack_fee = '$_POST[pack_fee]', free_money= '$_POST[free_money]', pack_desc = '$_POST[pack_desc]' ";
    
    /* 处理图片 */	 
    $img_name = '';
    if (!empty($_FILES['pack_img']))
    {
	    $ftp->connect();
	    $ftp->set_updata(TRUE, $_POST['id'], 'pack');
	    $affix_data = $ftp->put_up($_FILES['pack_img']);
	    if ($affix_data !== false)
	    {
	        $img_name = $affix_data['virtual_path'];
	    }
	    $ftp->quit();
    }
    
    if (!empty($img_name))
    {
        $param .= " ,pack_img = '$img_name' ";
    }

    if ($exc->edit($param ,  $_POST['id']))
    {
        $link[0]['text'] = $_LANG['back_list'];
        $link[0]['href'] = 'pack.php?act=list&' . list_link_postfix();
        $note = sprintf($_LANG['packedit_succed'], $_POST['pack_name']);
        sys_msg($note, 0, $link);
        admin_log($_POST['pack_name'], 'edit', 'pack');
    }
    else
    {
        die($db->error());
    }

}

/* 删除卡片图片 */
if ($_REQUEST['act'] == 'drop_pack_img')
{
    /* 权限判断 */
    admin_priv('drop_pack');
    
    $pack_id = isset($_GET['id']) ? intval($_GET['id']) : 0;

    /* 取得logo名称 */
    $sql = "SELECT pack_img FROM " .$os->table('pack'). " WHERE pack_id = '$pack_id'";
    $img_name = $db->getOne($sql);

    if (!empty($img_name))
    {
	    $ftp->connect();
	    @$ftp->drop($pack_id, 'pack');
	    $ftp->quit();	
        $sql = "UPDATE " .$os->table('pack'). " SET pack_img = '' WHERE pack_id = '$pack_id'";
        $db->query($sql);
    }
    $link= array(array('text' => $_LANG['pack_edit_lnk'], 'href'=>'pack.php?act=edit&id=' .$pack_id), array('text' => $_LANG['pack_list_lnk'], 'href'=>'pack.php?act=list'));
     sys_msg($_LANG['drop_pack_img_success'], 0, $link);
}

/*------------------------------------------------------ */
//-- 编辑包装名称
/*------------------------------------------------------ */

if ($_REQUEST['act'] == 'edit_name')
{
    check_authz_json('edit_pack');

    $id = intval($_POST['id']);
    $val = json_str_iconv(trim($_POST['val']));

    /* 取得该属性所属商品类型id */
    $pack_name = $exc->get_name($id);

    if (!$exc->is_only('pack_name', $val, $id))
    {
        make_json_error(sprintf($_LANG['packname_exist'], $pack_name));
    }
    else
    {
        $exc->edit("pack_name='$val'", $id);

        admin_log($val, 'edit', 'pack');
        make_json_result(stripslashes($val));
    }
}

/*------------------------------------------------------ */
//-- 编辑包装费用
/*------------------------------------------------------ */

if ($_REQUEST['act'] == 'edit_pack_fee')
{
    check_authz_json('edit_pack');

    $id = intval($_POST['id']);
    $val = floatval($_POST['val']);

    /* 取得该属性所属商品类型id */
    $pack_name = $exc->get_name($id);

    $exc->edit("pack_fee='$val'", $id);
    admin_log(addslashes($pack_name), 'edit', 'pack');
    make_json_result(number_format($val, 2));
}

/*------------------------------------------------------ */
//-- 编辑免费额度
/*------------------------------------------------------ */

if ($_REQUEST['act'] == 'edit_free_money')
{
    check_authz_json('edit_pack');

    $id = intval($_POST['id']);
    $val = floatval($_POST['val']);

    /* 取得该属性所属商品类型id */
    $pack_name = $exc->get_name($id);

    $exc->edit("free_money='$val'", $id);
    admin_log(addslashes($pack_name), 'edit', 'pack');
    make_json_result(number_format($val, 2));
}

/*------------------------------------------------------ */
//-- 删除包装
/*------------------------------------------------------ */

if ($_REQUEST['act'] == 'remove')
{
    check_authz_json('drop_pack');

    $id     = intval($_GET['id']);
    $name   = $exc->get_name($id);

    if ($exc->drop($id))
    {
        /* 删除图片 */
        if ($id>0 && !empty($name))
        {
		    $ftp->connect();
		    @$ftp->drop($id, 'pack');
		    $ftp->quit();	        
        }
        admin_log(addslashes($name),'remove','pack');

        $url = 'pack.php?act=query&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);

        os_header("Location: $url\n");
        exit;
    }
    else
    {
        make_json_error($_LANG['packremove_falure']);
        return false;
    }
}

function packs_list()
{
    $result = get_filter();
    if ($result === false)
    {
        $filter['sort_by']      = empty($_REQUEST['sort_by']) ? 'pack_id' : trim($_REQUEST['sort_by']);
        $filter['sort_order']   = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);

        $sql = "SELECT count(*) FROM " .$GLOBALS['os']->table('pack');
        $filter['record_count'] = $GLOBALS['db']->getOne($sql);

        /* 分页大小 */
        $filter = page_and_size($filter);

        /* 查询 */
        $sql = "SELECT pack_id, pack_name, pack_img, pack_fee, free_money, pack_desc".
               " FROM ".$GLOBALS['os']->table('pack').
               " ORDER by " . $filter['sort_by'] . ' ' . $filter['sort_order'] .
               " LIMIT " . $filter['start'] . ',' . $filter['page_size'];

        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }

    $packs_list = $GLOBALS['db']->getAll($sql);

    $arr = array('packs_list' => $packs_list, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);

    return $arr;
}
?>